# Maintainer: Carlos Antunes <cmantunes@gmail.com>
# Contributed by: nanodude

_realname=newlib
_target=arm-none-eabi

pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_target}-${_realname}")
pkgver=2.1.0
pkgrel=1
pkgdesc="GNU Tools for ARM Embedded Processors - Newlib (mingw-w64)"
arch=('any')
url="https://sourceware.org/newlib/"
license=('Various')
groups=("${MINGW_PACKAGE_PREFIX}-${_target}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-${_target}-binutils")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' '!strip' '!emptydirs')
source=('gcc-arm-embedded.tar.bz2::https://launchpad.net/gcc-arm-embedded/4.8/4.8-2014-q3-update/+download/gcc-arm-none-eabi-4_8-2014q3-20140805-src.tar.bz2'
        '10-newlib-arm-dynamic-reent.patch'
        '20-newlib-libgloss-configure-fix.patch'
	'99-gcc-isl-0.13.patch')
sha256sums=('09519946642d5366ab739a89d3388b2ea333cd7dc50192bab3736a9593af83e6'
            '7522f26659a69e2f02eb37d3557f8dca08bc32ada8c797b6a812ad131c87b0ef'
            '3222e2910068d413c23b2830cf2c9b7b2db210c9b98c5644db5934fee84d9808'
            '8e8da71e6e6b347dd7fc7f7938bf9af22be031ad8916749fa6c368f87d8891f0')
noextract=('gcc-arm-embedded.tar.bz2')

prepare() {
  # Extract packages from archive
  rm -rf ${srcdir}/tmp
  mkdir ${srcdir}/tmp && cd ${srcdir}/tmp
  tar xf ../../gcc-arm-embedded.tar.bz2 --strip-components=2

  # Copy and extract GCC and Newlib packages
  cp gcc.tar.bz2 ${srcdir}
  cp newlib.tar.bz2 ${srcdir}
  cd ${srcdir}
  [ -d gcc ] && rm -rf gcc
  [ -d newlib ] && rm -rf newlib
  tar xf gcc.tar.bz2
  tar xf newlib.tar.bz2

  # Newlib
  cd ${srcdir}/${_realname}
  patch -p1 -i "${srcdir}/10-newlib-arm-dynamic-reent.patch"
  patch -p2 -i "${srcdir}/20-newlib-libgloss-configure-fix.patch"

  # GCC: Compatibility with isl>=0.12.2
  cd "${srcdir}/gcc"
  patch -p0 -i "${srcdir}/99-gcc-isl-0.13.patch"

  # Build cross-compiler just to compile newlib
  rm -rf "${srcdir}/compiler-${MINGW_CHOST}"
  mkdir -p "${srcdir}/compiler-${MINGW_CHOST}"
  cp -a "${MINGW_PREFIX}/${_target}" "${srcdir}/compiler-${MINGW_CHOST}"
  cd $srcdir
  rm -rf build-gcc-${MINGW_CHOST}
  mkdir -p build-gcc-${MINGW_CHOST} && cd build-gcc-${MINGW_CHOST}
  ../gcc/configure \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${_target}" \
    --prefix="${srcdir}/compiler-${MINGW_CHOST}" \
    --enable-languages="c" \
    --disable-werror \
    --disable-lto \
    --disable-nls \
    --disable-libffi \
    --disable-decimal-float \
    --disable-libgomp \
    --disable-libmudflap \
    --disable-libquadmath \
    --disable-libssp \
    --disable-libstdcxx-pch \
    --disable-libsanitizer \
    --disable-threads \
    --disable-tls \
    --disable-shared \
    --enable-multilib \
    --enable-interwork \
    --with-newlib \
    --without-headers \
    --with-gnu-as \
    --with-gnu-ld \
    --with-system-zlib \
    --with-{gmp,mpfr,mpc,isl,cloog}="${MINGW_PREFIX}" \
    --disable-isl-version-check \
    --disable-cloog-version-check \
    --with-libelf \
    --with-multilib-list="armv6-m,armv7-m,armv7e-m,armv7-r" \
    --with-sysroot="${MINGW_PREFIX}" \
    --with-build-sysroot="${srcdir}/compiler-${MINGW_CHOST}" \
    --with-build-time-tools="${MINGW_PREFIX}/${_target}/bin"
  make all-gcc
  make -j1 install-gcc
}

build() {
  # Build newlib
  local armgccpath="${srcdir}/compiler-${MINGW_CHOST}/bin"
  if check_option "debug" "y"; then
    NEWLIB_CFLAGS="-O0"
  else
    NEWLIB_CFLAGS="-O3"
  fi
  NEWLIB_CFLAGS+=" -g -ffunction-sections -fdata-sections"
#  NEWLIB_CFLAGS+=" -DMALLOC_PROVIDED"
#  NEWLIB_CFLAGS+=" -DREENTRANT_SYSCALLS_PROVIDED"
  cd $srcdir
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../${_realname}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${_target} \
    --prefix=${MINGW_PREFIX} \
    --enable-newlib-io-pos-args \
    --enable-newlib-io-c99-formats \
    --enable-newlib-io-long-long \
    --enable-newlib-io-float \
    --enable-newlib-reent-small \
    --disable-newlib-supplied-syscalls \
    --disable-nls \
    CFLAGS="${NEWLIB_CFLAGS}" \
    PATH="${armgccpath}:${PATH}"
  make PATH="${armgccpath}:${PATH}"
}

package() {
  local armgccpath="${srcdir}/compiler-${MINGW_CHOST}/bin"
  cd ${srcdir}/build-${MINGW_CHOST}
  make -j1 DESTDIR=${pkgdir} PATH="${armgccpath}:${PATH}" install

  # Remove files that conflict with host
  rm -rf "${pkgdir}${MINGW_PREFIX}/share/info"
}

